<?php

/**
 * Ventas form base class.
 *
 * @package    tercermilenio
 * @subpackage form
 * @author     Your name here
 */
class BaseVentasForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'vta_id'              => new sfWidgetFormInputHidden(),
      'vta_nro_factura'     => new sfWidgetFormInput(),
      'vta_cliente'         => new sfWidgetFormPropelChoice(array('model' => 'Clientes', 'add_empty' => false)),
      'vta_fecha'           => new sfWidgetFormDate(),
      'vta_tipo'            => new sfWidgetFormPropelChoice(array('model' => 'TipoVentas', 'add_empty' => false)),
      'vta_total'           => new sfWidgetFormInput(),
      'vta_usuario'         => new sfWidgetFormPropelChoice(array('model' => 'sfGuardUser', 'add_empty' => false)),
      'vta_iva'             => new sfWidgetFormInput(),
      'vta_descuento'       => new sfWidgetFormInput(),
      'vta_efectivo'        => new sfWidgetFormInput(),
      'vta_cheque'          => new sfWidgetFormInput(),
      'vta_cta_cte'         => new sfWidgetFormInput(),
      'vta_cuotas'          => new sfWidgetFormInput(),
      'vta_tarjeta'         => new sfWidgetFormInput(),
      'vta_desc_aplicado'   => new sfWidgetFormInput(),
      'detalle_ventas_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'Productos')),
    ));

    $this->setValidators(array(
      'vta_id'              => new sfValidatorPropelChoice(array('model' => 'Ventas', 'column' => 'vta_id', 'required' => false)),
      'vta_nro_factura'     => new sfValidatorString(array('max_length' => 20)),
      'vta_cliente'         => new sfValidatorPropelChoice(array('model' => 'Clientes', 'column' => 'cli_id')),
      'vta_fecha'           => new sfValidatorDate(),
      'vta_tipo'            => new sfValidatorPropelChoice(array('model' => 'TipoVentas', 'column' => 'tip_vta_id')),
      'vta_total'           => new sfValidatorNumber(),
      'vta_usuario'         => new sfValidatorPropelChoice(array('model' => 'sfGuardUser', 'column' => 'id')),
      'vta_iva'             => new sfValidatorNumber(),
      'vta_descuento'       => new sfValidatorNumber(),
      'vta_efectivo'        => new sfValidatorNumber(array('required' => false)),
      'vta_cheque'          => new sfValidatorNumber(array('required' => false)),
      'vta_cta_cte'         => new sfValidatorNumber(array('required' => false)),
      'vta_cuotas'          => new sfValidatorInteger(array('required' => false)),
      'vta_tarjeta'         => new sfValidatorNumber(array('required' => false)),
      'vta_desc_aplicado'   => new sfValidatorInteger(array('required' => false)),
      'detalle_ventas_list' => new sfValidatorPropelChoiceMany(array('model' => 'Productos', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('ventas[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Ventas';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['detalle_ventas_list']))
    {
      $values = array();
      foreach ($this->object->getDetalleVentass() as $obj)
      {
        $values[] = $obj->getDetVtaArticulo();
      }

      $this->setDefault('detalle_ventas_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveDetalleVentasList($con);
  }

  public function saveDetalleVentasList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['detalle_ventas_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(DetalleVentasPeer::DET_VTA_VTA, $this->object->getPrimaryKey());
    DetalleVentasPeer::doDelete($c, $con);

    $values = $this->getValue('detalle_ventas_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new DetalleVentas();
        $obj->setDetVtaVta($this->object->getPrimaryKey());
        $obj->setDetVtaArticulo($value);
        $obj->save();
      }
    }
  }

}
